import math
from openpyxl import load_workbook

X_INTERNAL = 0.02 * 1852


def from_depth_to_fake_width(depth):
    return 2 * depth * math.tan(math.pi / 3)


def from_depth_list_to_avg_width(depth_list):
    sum = 0
    for depth in depth_list:
        sum += from_depth_to_fake_width(depth)
    return sum / len(depth_list)


def from_xlsx_to_cols():
    wb = load_workbook("附件.xlsx")
    ws = wb.active
    result = []
    temp_list = []
    for col_i in range(3, 204):
        for row_i in range(3, 254):
            temp_list.append(ws.cell(row=row_i, column=col_i).value)
        result.append(temp_list[:])
        temp_list.clear()
    return result


def from_xlsx_to_rows():
    wb = load_workbook("附件.xlsx")
    ws = wb.active
    result = []
    temp_list = []
    for row_i in range(3, 254):
        for col_i in range(3, 204):
            temp_list.append(ws.cell(row=row_i, column=col_i).value)
        result.append(temp_list[:])
        temp_list.clear()
    return result


def from_index_to_x_position(index):
    return X_INTERNAL * index


def four():
    # result
    col_lists = from_xlsx_to_cols()
    avg_width_lists = []
    for col_list in col_lists:
        avg_width_lists.append(from_depth_list_to_avg_width(col_list))
    measure_line_index_list = []
    x_0_index = 0
    while avg_width_lists[x_0_index] / 2 > from_index_to_x_position(x_0_index):
        x_0_index += 1
    x_0_index -= 1
    measure_line_index_list.append(x_0_index)
    x_1_index = x_0_index + 1
    while (
        from_index_to_x_position(x_1_index) + from_depth_to_fake_width(x_1_index)
    ) < 4 * 1852:
        while True:
            middle_width = (
                avg_width_lists[x_1_index]
                + avg_width_lists[x_0_index]
                - X_INTERNAL * (x_1_index - x_0_index)
            )
            x_1_index += 1
            if middle_width <= avg_width_lists[x_0_index] * 0.8:
                measure_line_index_list.append(x_1_index)
                x_0_index = x_1_index
                x_1_index += 1
                break
    print("measure_line_index_list", measure_line_index_list)
    print("len(measure_line_index_list)", len(measure_line_index_list))
    # nocover
    nocover_num = 0
    in_flag = False
    row_lists = from_xlsx_to_rows()
    for row_list_index in range(250):
        row_list = row_lists[row_list_index]
        for now_col_index in range(200):
            in_flag = False
            for measure_line_col_index in measure_line_index_list:
                measure_line_depth = row_list[measure_line_col_index]
                measure_width = from_depth_to_fake_width(measure_line_depth)
                left = X_INTERNAL * measure_line_col_index - measure_width / 2
                right = X_INTERNAL * measure_line_col_index + measure_width / 2
                if (
                    left <= X_INTERNAL * now_col_index
                    and right >= X_INTERNAL * now_col_index
                ):
                    in_flag = True
                    break
            if not in_flag:
                nocover_num += 1
    print("nocover_num", nocover_num)
    # over_length
    over_num = 0
    for measure_line_col_index_index in range(0, len(measure_line_index_list) - 1):
        measure_line_col_index = measure_line_index_list[measure_line_col_index_index]
        for row_list_index in range(250):
            row_list = row_lists[row_list_index]
            measure_line_depth = row_list[measure_line_col_index]
            measure_width = from_depth_to_fake_width(measure_line_depth)
            left_index = measure_line_col_index - measure_width / 2 / X_INTERNAL
            right_index = measure_line_col_index + measure_width / 2 / X_INTERNAL
            all_num = right_index - left_index + 1

            another_measure_line_depth = row_list[
                measure_line_index_list[measure_line_col_index_index + 1]
            ]
            another_measure_width = from_depth_to_fake_width(another_measure_line_depth)
            another_left_index = (
                measure_line_index_list[measure_line_col_index_index + 1]
                - another_measure_width / 2 / X_INTERNAL
            )
            if (
                another_left_index < right_index
                and (right - another_left_index) > all_num * 0.2
            ):
                over_num += 1
    print("over_num", over_num)


if __name__ == "__main__":
    four()
